package view;

import java.awt.Container;
import java.beans.PropertyVetoException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JInternalFrame;

public class TelaPrincipal extends javax.swing.JFrame {

    ArrayList<JInternalFrame> listaTelas = new ArrayList<>();
    String nome;
    String cargo;
    String id;
    String senha;
    
    public TelaPrincipal() {
        initComponents();
        this.setTitle("Sistema Gerenciador de Bar");
        this.setVisible(true);
        this.setExtendedState(this.MAXIMIZED_BOTH);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDesktopPane1 = new javax.swing.JDesktopPane();
        jToolBar2 = new javax.swing.JToolBar();
        jCliente = new javax.swing.JButton();
        jProduto = new javax.swing.JButton();
        jRelatorio = new javax.swing.JButton();
        jFuncionario = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLancamento = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jDesktopPane1.setBackground(new java.awt.Color(204, 204, 204));

        javax.swing.GroupLayout jDesktopPane1Layout = new javax.swing.GroupLayout(jDesktopPane1);
        jDesktopPane1.setLayout(jDesktopPane1Layout);
        jDesktopPane1Layout.setHorizontalGroup(
            jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jDesktopPane1Layout.setVerticalGroup(
            jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 266, Short.MAX_VALUE)
        );

        jToolBar2.setRollover(true);

        jCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cliente2.png"))); // NOI18N
        jCliente.setText("Gerenciarclientes");
        jCliente.setFocusable(false);
        jCliente.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jCliente.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jClienteActionPerformed(evt);
            }
        });
        jToolBar2.add(jCliente);

        jProduto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/produto2.png"))); // NOI18N
        jProduto.setText("Gerenciar produtos");
        jProduto.setFocusable(false);
        jProduto.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jProduto.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jProduto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jProdutoActionPerformed(evt);
            }
        });
        jToolBar2.add(jProduto);

        jRelatorio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/relatorio2.png"))); // NOI18N
        jRelatorio.setText("Gerar relatórios");
        jRelatorio.setFocusable(false);
        jRelatorio.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jRelatorio.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jRelatorio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRelatorioActionPerformed(evt);
            }
        });
        jToolBar2.add(jRelatorio);

        jFuncionario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/funcionario2.png"))); // NOI18N
        jFuncionario.setText("Gerenciar funcionários");
        jFuncionario.setFocusable(false);
        jFuncionario.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jFuncionario.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jFuncionario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFuncionarioActionPerformed(evt);
            }
        });
        jToolBar2.add(jFuncionario);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Conta p.png"))); // NOI18N
        jButton1.setText("Gerenciar contas");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar2.add(jButton1);

        jLancamento.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/lancamento2.png"))); // NOI18N
        jLancamento.setText("Realizar lançamentos");
        jLancamento.setFocusable(false);
        jLancamento.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLancamento.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jLancamento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jLancamentoActionPerformed(evt);
            }
        });
        jToolBar2.add(jLancamento);

        jMenu1.setText("File");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Sair");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Opções");

        jMenuItem2.setText("Alterar senha");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane1)
            .addComponent(jToolBar2, javax.swing.GroupLayout.DEFAULT_SIZE, 598, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jDesktopPane1))
        );

        setSize(new java.awt.Dimension(614, 412));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jFuncionarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFuncionarioActionPerformed
        TelaGerenciarFuncionario t = new TelaGerenciarFuncionario();
        addTela(t.getContentPane(), "Gerenciar funcionários");
    }//GEN-LAST:event_jFuncionarioActionPerformed

    private void jRelatorioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRelatorioActionPerformed
        TelaRelatorio r = new TelaRelatorio();
        addTela(r.getContentPane(), "Gerar relatórios");
    }//GEN-LAST:event_jRelatorioActionPerformed

    private void jLancamentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jLancamentoActionPerformed
        try {
            TelaLancamento l = new TelaLancamento();
            addTela(l.getContentPane(), "Lançamentos");
        } catch (SQLException ex) {
            Logger.getLogger(TelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jLancamentoActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jProdutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jProdutoActionPerformed
        TelaGerenciarProduto p = new TelaGerenciarProduto();
        addTela(p.getContentPane(), "Gerenciar produtos");
    }//GEN-LAST:event_jProdutoActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        TelaAlterarSenha alterarSenha = new TelaAlterarSenha();
        addTela(alterarSenha.getContentPane(), "Alterar senha");
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jClienteActionPerformed
        TelaGerenciarCliente p = new TelaGerenciarCliente();
        addTela(p.getContentPane(), "Gerenciar clientes");
    }//GEN-LAST:event_jClienteActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        TelaGerenciarConta alterarSenha = new TelaGerenciarConta();
        addTela(alterarSenha.getContentPane(), "Gerenciar conta");
    }//GEN-LAST:event_jButton1ActionPerformed

    public void addTela(Container tela, String titulo){
        final JInternalFrame internalFrame = new JInternalFrame(titulo, true, true, true, true);
        internalFrame.setContentPane(tela);
        internalFrame.pack();
        internalFrame.setVisible(true);
        
        int lDesk = jDesktopPane1.getWidth();       
        int aDesk = jDesktopPane1.getHeight();       
        int lIFrame = internalFrame.getWidth();      
        int aIFrame = internalFrame.getHeight();
        internalFrame.setLocation( lDesk / 2 - lIFrame / 2, aDesk / 2 - aIFrame / 2 );
        
        this.listaTelas.add(internalFrame);
        this.jDesktopPane1.add(internalFrame);
        
        try {
            internalFrame.setSelected(true);
        } catch (PropertyVetoException ex) {
            Logger.getLogger(TelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }
        internalFrame.moveToFront();
        internalFrame.setResizable(false);
        
        this.repaint();
    }
    
    public void removeTela(Class tela){
        int index = 0;
        for (int i = 0; i < listaTelas.size(); i++) {
            if(listaTelas.get(i).getClass().equals(tela)){
                index = i;
                break;
            }
        }
        this.listaTelas.remove(index);
        this.jDesktopPane1.remove(index);
        this.repaint();
    }
    
    public void setLogado(String id, String nome, String cargo, String senha){
        this.nome = nome;
        this.cargo = cargo;
        this.id = id;
        this.senha = senha;
        
        if(cargo.equals("Garçom")){
            this.jCliente.setVisible(false);
            this.jProduto.setVisible(false);
            this.jRelatorio.setVisible(false);
            this.jFuncionario.setVisible(false);
        }
        if(cargo.equals("Caixa")){
            this.jProduto.setVisible(false);
            this.jRelatorio.setVisible(false);
            this.jFuncionario.setVisible(false);
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
//                new TelaPrincipal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jCliente;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JButton jFuncionario;
    private javax.swing.JButton jLancamento;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JButton jProduto;
    private javax.swing.JButton jRelatorio;
    private javax.swing.JToolBar jToolBar2;
    // End of variables declaration//GEN-END:variables
}
